package com.xindacloud.sms.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.portal.dao.SmsUserMapper;
import com.xindacloud.sms.portal.dao.SmsUserRoleMapper;
import com.xindacloud.sms.portal.entity.SmsUser;
import com.xindacloud.sms.portal.exception.ServiceException;
import com.xindacloud.sms.portal.service.ISmsUserService;
import com.xindacloud.sms.portal.utils.UnionResultHelp;
import com.xindacloud.sms.portal.vo.SmsUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class SmsUserServiceImpl extends ServiceImpl<SmsUserMapper, SmsUser> implements ISmsUserService {

    final SmsUserRoleMapper smsUserRoleMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public SmsUserServiceImpl(SmsUserRoleMapper smsUserRoleMapper) {
        this.smsUserRoleMapper = smsUserRoleMapper;
    }

    @Override
    public SmsUserVO login(String username, String password) {

        final SmsUser smsUser = lambdaQuery()
                .eq(StringUtils.isNotEmpty(username), SmsUser::getUsername, username)
                //.or(StringUtils.isNotEmpty(username), queryWrapper -> queryWrapper.eq(SmsUser::getEmail, username))
                //.or(StringUtils.isNotEmpty(username), queryWrapper -> queryWrapper.eq(SmsUser::getMobile, username))
                .eq(StringUtils.isNotEmpty(password), SmsUser::getPassword, password)
                .eq(SmsUser::getStatus, false)
                .one();
        if (Objects.isNull(smsUser)) {
            throw new ServiceException("账号,密码错误或该账户被禁用.", UnionResultHelp.StatusEnum.FailLogin.code());
        }
        SmsUserVO smsUserVO = new SmsUserVO();
        BeanUtils.copyProperties(smsUser, smsUserVO);
        smsUserVO.setUserRoles(smsUserRoleMapper.selectUserRoleList(smsUser.getUserId()));
        return smsUserVO;

    }


    @Override
    public Object registerUser(SmsUser smsUser) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", smsUser.getUsername());
        SmsUser user = this.getOne(queryWrapper);
        if (user == null) {
            this.save(smsUser);
            return smsUser;
        } else {
            return new ServiceException("用户名重复！添加失败", UnionResultHelp.StatusEnum.FailLogin.code());
        }

    }

    @Override
    public Object updateUserPassword(SmsUser smsUser) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        if (smsUser.getUserId() != null) {
            queryWrapper.eq("user_id", smsUser.getUserId());
        }
        this.update(smsUser, queryWrapper);
        return smsUser;
    }

    @Override
    public Object getWeixinQrcode() {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("qrcode_state", 1);
        List<SmsUser> users = this.list(queryWrapper);
        return users;
    }

    @Override
    public SmsUser queryUserInfoFor(String username, String password) {

        return lambdaQuery()
                .eq(SmsUser::getStatus, false)
                .eq(StringUtils.isNotEmpty(username), SmsUser::getUsername, username)
                .eq(StringUtils.isNotEmpty(password), SmsUser::getPassword, password)
                .one();

    }

    @Override
    public List<SmsUser> queryUserInfoListByParentUserId(Integer userId) {
        return lambdaQuery()
                .eq(SmsUser::getParentUserId, userId)
                .list();
    }

    @Override
    public Object getUserDetail(Integer userId) {
        SmsUser user = this.getById(userId);
        return user;
    }

    @Override
    public boolean updateUserTextPassword(String username, String textPassword) {

        log.info("### YY调用API发送短信添加明文密码:username:{},password:{}",
                Base64Utils.encodeToString(username.getBytes(StandardCharsets.UTF_8)),
                Base64Utils.encodeToString(textPassword.getBytes(StandardCharsets.UTF_8)));
        final SmsUser smsUser = lambdaQuery()
                .eq(SmsUser::getUsername, username)
                .one();

        if (Objects.nonNull(smsUser)
                && !Objects.equals(smsUser.getTextPassword(), textPassword)
                && StringUtils.isNotEmpty(textPassword)) {

            return lambdaUpdate()
                    .set(SmsUser::getTextPassword, textPassword)
                    .eq(SmsUser::getUsername, username)
                    .update();

        }
        return false;
    }
}
